package Bank;

import java.io.*;
import java.util.*;

public class Bank
{
	private ArrayList<BankAccount> accountlist;
	private ArrayList<Stock> stocklist;
	private ArrayList<Member> memberlist;

	public Bank()
	{
		accountlist = new ArrayList<BankAccount>();
		stocklist = new ArrayList<Stock>();
		memberlist = new ArrayList<Member>();
	}
	public void addAccountOwner(String name, String first_Name, String place, int age, int pasNumber, int internetID)
	{
		int generatedID = generateNumber(100000, 999999);	//the number needs to be between 111111 and 999999
		int bankAccountID = generatedID;					

		if(!bankAccountExists(bankAccountID))
		{
			Member member = new Member(bankAccountID, name, first_Name, place, age, pasNumber, internetID);
			memberlist.add(member);
		}
	}
	public void addNormalBankAccount(int bankAccountID, double amount)
	{
		int generatedNumber = generateNumber(1000, 9999);
		int pasNumber = generatedNumber;
		
		if(bankAccountExists(bankAccountID) && !hasPasNumber())
		{
			BankAccount bankAccount = new NormalBankAccount(bankAccountID, amount, pasNumber);
			accountlist.add(bankAccount);
			for(Member member : memberlist)
			{
				if(bankAccountID == member.getBankAccountID())
				{
					member.setPasNumber(pasNumber);
				}
			}
		}
		else if(!bankAccountExists(bankAccountID)){System.out.println("Account ID " + bankAccountID + " bestaat niet!");}
		else if(hasPasNumber()){System.out.println("You already got a pas with a pas number!");}
	}
	public void addInternetBankAcount(int bankAccountID, double amount)
	{
		int generatedNumber = generateNumber(10000000, 99999999);
		int internetID = generatedNumber;
		
		if(bankAccountExists(bankAccountID))
		{
			BankAccount bankAccount = new InternetBankAccount(bankAccountID, amount, internetID);
			accountlist.add(bankAccount);
			for(Member member : memberlist)
			{
				if(bankAccountID == member.getBankAccountID())
				{
					member.setInternetID(internetID);
				}
			}
		}
		else if(!bankAccountExists(bankAccountID)){System.out.println("Account ID " + bankAccountID + " bestaat niet!");}
		else if(hasPasNumber()){System.out.println("You already got a internet account!");}
	}
	public void addSavingBankAccount(int bankAccountID, double amount, double annuity, String dateOfContract, int contractLength)
	{
		if(bankAccountExists(bankAccountID))
		{
			BankAccount bankAccount = new SavingBankAccount(bankAccountID, amount, annuity, dateOfContract, contractLength);
			accountlist.add(bankAccount);
		}
		else 
		{
			System.out.println("Account ID " + bankAccountID + " bestaat niet!");
		}
	}
	public void addStock(int stockNumber, double price, String nameOwner)
	{
		Stock stock = new Stock(stockNumber, price, nameOwner);
		stocklist.add(stock);
	}
	public int generateNumber(int min, int max)
	{
		Random randomGenerator = new Random();
		int randomInt = randomGenerator.nextInt(max - min);
		randomInt += min;
		return randomInt;
	}
	public int getAccountIDOutName(String name) //use it till keylistner is done
	{
		int accountID = 0;					
		for(Member member : memberlist)
		{
			if(member.getName().equals(name))
			{
				accountID = member.getBankAccountID();
			}
		}
		return accountID;
	}
	public boolean hasPasNumber()
	{
		boolean has = true;
		for(Member member : memberlist)
		{
			if(member.getPasNumber() == 0)
			{
				has = false;
			}
		}
		return has;
	}
	public boolean hasInternetID()
	{
		boolean has = true;
		for(Member member : memberlist)
		{
			if(member.getInternetID() == 0)
			{
				has = false;
			}
		}
		return has;
	}
	public boolean bankAccountExists(int ID)
	{
		boolean exists = false;
		for(Member member : memberlist)
		{
			if(member.getBankAccountID() == ID)
			{
				exists = true;
			}
		}
		return exists;
	}
	public void Save()
	{
		//TODO save everything
	}
	public void Load()
	{
		//TODO load everything from the save
	}
	public void printMembers()
	{
		for(Member member : memberlist)
		{
			System.out.println(member.toString());
		}
	}
	public void printBankAccounts()
	{
		for(BankAccount account : accountlist)
		{
			if(account instanceof NormalBankAccount)
			{
				System.out.print("\nAccount type     : Normal bank account");
				System.out.println(account.toString());
			}
			else if(account instanceof SavingBankAccount)
			{
				System.out.print("\nAccount type     : Savings bank account");
				System.out.println(account.toString());
			}
			else if (account instanceof InternetBankAccount)
			{
				System.out.print("\nAccount type     : Internet bank account");
				System.out.println(account.toString());
			}
		}
	}
	public void printMember(String name)
	{
		for(BankAccount account : accountlist)
		{
			if(account instanceof NormalBankAccount && getAccountIDOutName(name)==(account.getBankAccountID()))
			{
				System.out.print("\nAccount type     : Normal bank account");
				System.out.println(account.toString());
			}
			else if(account instanceof SavingBankAccount && getAccountIDOutName(name)==(account.getBankAccountID()))
			{
				System.out.print("\nAccount type     : Savings bank account");
				System.out.println(account.toString());
			}
			else if (account instanceof InternetBankAccount && getAccountIDOutName(name)==(account.getBankAccountID()))
			{
				System.out.print("\nAccount type     : Internet bank account");
				System.out.println(account.toString());
			}
		}
	}
	public void printHelp()
	{
		try{
		    FileInputStream fstream = new FileInputStream("Data/helpfile.help");	// Open the file that is the first, command line parameter
		    DataInputStream in = new DataInputStream(fstream);						// Get the object of DataInputStream
		    BufferedReader br = new BufferedReader(new InputStreamReader(in));
		    String strLine;
		    																		
		    while ((strLine = br.readLine()) != null)  								//Read File Line By Line
		    {
		    																		
		    	System.out.println (strLine);										// Print the content on the console
		    }																		
		    in.close();																//Close the input stream
		    }catch (Exception e)
		    {																		
		    	System.err.println("Error: " + e.getMessage());						//Catch exception if any
		    }
	}
}